<?php
namespace console\controllers;

use yii;
use yii\console\Controller;
use Codeception\Step\Condition;

/**
 * 专门用于检查迁移通讯录的
 * @author mayn
 *
 */
class MemberPhoneController extends Controller
{
    private $master;
    
    //有问题的
    public $userids = [
        '1'=>'548961,549114,549160,549224,549295,549448,552411,554267,558054,558112,558333,558946,559169,559355,561501,562945,563885,564926,566258,567219,567700,569097,569496,569931,571145,571526,571965,572491,572701,573502,573611,573716,575479,575647,575786,576749,577110,578804,579070,579967,580713,580720,581504,583319,584787,585310,587387,587936,588105,588791,590063,591408,594984,594985,594382,594979,594980,594981,594988,594989,594990,594991,594994,594995,594998,594999,595000,595001,595004,595005,595008,595009,595010,595011,595014,595015,595018,595020,595021,595024,595025,595028,595030,595031,595034,595035,595038,595040,595041,595044,595045,595048,595050,595051,595054,595055,595058,595060,595061,595064',
        '2'=>'595065,595068,595070,595071,595074,595075,595078,595080,595081,595084,595085,595088,595090,595091,595094,595095,595098,595100,595101,595104,595105,595108,595110,595111,595114,595115,595118,595120,595121,595124,595125,595128,595130,595131,595134,595135,595138,595140,595141,595144,595145,595148,595150,595151,595154,595155,595160,595161,595164,595165,595170,595171,595174,595175,594982,594983,594986,594987,594992,594993,594996,594997,595002,595003,595006,595007,595012,595013,595016,595017,595019,595022,595023,595026,595027,595029,595032,595033,595036,595037,595039,595042,595043,595046,595047,595049,595052,595053,595056,595057,595059,595062,595063,595066,595067,595069,595072,595073,595076,595077',
        '3'=>'595079,595082,595083,595086,595087,595089,595092,595093,595096,595097,595099,595102,595103,595106,595107,595109,595112,595113,595117,595119,595122,595123,595127,595129,595132,595133,595137,595139,595142,595143,595147,595149,595152,595153,595157,595158,595159,595162,595163,595167,595168,595169,595172,595173,595177,595178,595971,595974,595975,595984,595985,595990,596001,595116,595126,595136,595146,595156,595166,595176,595967,595968,595969,595972,595973,595978,595983,595986,595987,595988,596016,596019,597274,597375,597612,597727,600633,601448,602929,603888,604503,608869,610847,615498,615613,615684,615905,617012,617191,617476,617685,618767,623183,625673,627235,627665,627668,627699,630647,635692',
        '4'=>'636128,636266,636857,636925,640749,641501,642417,642503,643896,648504,651095,653050,653800,654907,656067,657744,659693,664926,665185,665661,665761,666731,668640,668651,672142,675118,677753,679150,681201,681501,685714,686373,689128,690223,690440,691160,691161,691162,691163,691164,691165,691166,691167,691168,691169,691170,691171,691172,691173,691174,691175,691176,691177,691178,691179,691180,691181,691182,691183,691184,691185,691186,691187,691188,691189,691190,691191,691192,691193,691194,691195,691197,691198,691199,693401,693775,691196,697863,698420,699222,701331,704336,704337,704338,704339,704340,704341,704342,704343,704344,704345,704346,704347,704348,704349,704350,704351,704352,704353,704354',
        '5'=>'704355,704356,704357,704358,704359,704360,704361,704362,704363,704364,704365,704366,704367,704368,704369,704370,704371,704372,704373,704374,704375,704376,704377,704378,704379,704381,704382,704383,704385,704387,704388,704389,704390,704391,704392,704393,704394,706774,707541,707887,709757,710366,711630,711988,712026,712170,713473,713757,715972,716118,716794,717325,717536,719167,719567,720610,722029,722108,724145,724622,725701,726176,727428,727730,727969,729434,732179,733080,736674,737906,737974,738421,743363,743403,744159,744331,745716,746360,749341,750552,752282,753998,754814,755365,755544,756342,757597,759480,759501,759553,759951,760344,761186,762351,763151,765244,766310,768430,769877,770917',
        '6'=>'771468,772647,776246,778782,779670,781164,782579,783242,783630,783883,786068,786217,786916,787133,787661,788155,788426,788455,788778,789418,791385,791782,792343,793145,797529,797539,797549,797559,795227,797417,797528,797530,797531,797532,797533,797534,797535,797536,797537,797538,797540,797541,797542,797543,797544,797545,797546,797547,797548,797550,797551,797552,797553,797554,797555,797556,797557,797558,797560,797561,797562,797563,797564,797565,797566,797567,797568,797569,797570,797571,797572,797573,797574,797575,797576,797577,798613,799480,800140,800565,800631,802772,803501,804748,805506,805518,806271,807455,807472,807473,808618,809596,809939,810961,811217,811265,811737,815281,815741,816425',
        '7'=>'817007,817796,817841,818105,818600,820484,820745,821285,821453,822567,824241,823763,823975,824117,825670,826416,828763,829029,829667,830111,830158,830324,830984,833564,835429,837314,838679,838872,839901,840488,840622,841803,842141,842418,842795,843138,843582,843821,844706,848051,848092,849721,850091,850395,851748,852228,853436,854765,856685,858510,860476,860975,861056,861513,862136,862302,862698,863777,864336,867082,867763,868898,869318,869319,872834,873344,873628,875192,878573,880032,880356,880589,881352,881411,882336,884644,884928,886580,887691,888204,890490,890786,892292,892375,892782,893438,894145,896951,899783,900965,903639,906699,909496,910259,910550,911829,1044036,9192478,9193121,9194104',
        '8'=>'9195780,9195892,9196231,9196257,9196285,9196891,9196895,9196935,9197125,9197360,9197432,9197915,9198438,9198491,9198711,9199004,9199792,9199794,9201048,9201341,9204194,9205191,9205802,9205952,9207131,9207482,9207841,9207991,9208082,9209042,9210452,9196376,9197967,9200137,9201013,9204055,9204118,9205278,9205279,9207083,9207348,9207810,9208138,9208198,9208640,9208649,9208785,9208904,9209230,9209283,9209458,9209660,9209716,9209769,9209899,9210074,9210078,9210374,9210428,9210510,9210519,9210749,9210750,9210825,9210849,9211205,9211366,9211439,9211484,9211597,9212241,9212586,9212793,9213303,9214008,9214056,9214113,9214381,9214733,9214774,9215012,9215149,9215616,9215974,9216310,9216595,9216769,9216974,9217182,9217229,9217706,9217928,9218145,9218224,9218474,9219194,9219477,9219583,9219666,9220451',
        '9'=>'9220847,9220873,9220983,9221053,9221372,9221580,9221708,9221738,9221881,9221967,9223251,9223662,9223803,9223888,9224022,9224651,9224921,9225724,9225974,9229202,9229514,9229962,9230450,9230800,9231110,9231162,9231902,9232631,9233882,9236142,9236322,9219726,9220826,9222806,9225295,9225585,9225605,9225706,9226067,9227318,9227413,9229223,9229599,9229877,9230418,9230723,9230848,9231377,9231645,9232954,9233407,9233641,9234095,9234246,9234860,9234874,9234908,9235077,9235253,9235546,9235549,9235903,9235955,9237116,9237605,9237676,9237967,9238075,9238576,9238828,9239065,9239361,9239491,9239625,9239832,9239903,9240366,9240400,9240909,9240940,9241066,9241349,9241356,9241452,9241504', 
    ];
    
    public function init()
    {
        parent::init();
        
        $this->master = new yii\db\Connection([
            'dsn' => 'mysql:host=rr-bp1x1q15h8955237mo.mysql.rds.aliyuncs.com;dbname=xindaishuju',
            'username' => 'r0q9h2i2g7j1d2e5',
            'password' => 'w&6Ju+1Ij$5Sa+4Fh(7Dd!5Sc$0Yo!1C',
            'charset' => 'utf8',
            'tablePrefix' => 'busy_'
        ]);
        
    }

    /**
     * 数据格式检查
     */
    public function actionDataFormat($userid = 0, $gid = 0)
    {
        if ($userid) {
            $where = ['a.id'=>$userid];
        } else if($gid) {
            
            if (!array_key_exists($gid, $this->userids)) {
                exit("params error\n");
            }
            $where =  ['in', 'a.id', explode(',', $this->userids[$gid])];
     
        } else {
            exit("params error\n");
        }

        $list = (new \yii\db\Query())
            ->select(['a.id','a.phone','bid'=>'b.id'])
            ->from('busy_member_phone as a')
            ->leftJoin('busy_member as b', 'b.id = a.id')
            ->where($where)
            ->all($this->master);

            if (empty($list)){
                exit("data error\n");
            }
            
            foreach ($list as $key => $val) {
                
                if (!$val['bid']) {
//                     echo "\n".$key.'--'.str_pad('userid', 50, '-').$val['id']."\n";
                    continue;
                }
                
                $res = $this->address_list_filter($val['phone']);
                
                if ($res == null || (json_decode($res, true) === null)) {
                    echo "\n userid::".str_pad($val['id'], 50, '-')."\n";
                }
            }
    }
    
    
    function address_list_filter($data, $userid)
    {
        //是否处理成功
        $is_filter_success = true;
        
        if (empty($data) || empty($userid)) {
            return false;
        }
        
        if (!is_string($data)) {
            return false;
        }
        
        //判断通讯录里面是否有换行, ""退格；
        $search = ["\r\n", "\n", "\r",""];
        $data = str_replace($search, '', $data);
        
        //方案1，过滤掉emoji表情
        $data = preg_replace_callback('/./u', function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        }, $data);

        if (json_decode($data, true) !== null) {
            return $data;
        }
            
        //开始处理
        $data_tmp = trim($data, "[{}]");
        $data_tmp = explode("},{", $data_tmp);
        
        $r = [];
        foreach ($data_tmp  as $key => $val) {
            
            $val_tmp = "{" . $val . "}";
            
            if (json_decode($val_tmp, true) !== null) {
                continue;
            }
            
            //纠正数据，遇到的情况太多， 暂时不纠正
            //         $res = address_list_help($val);
            //         if ($res) {
            //             $data_tmp[$key] = $res;
            //             continue;
            //         }
            
            //没救了
            $is_filter_success = false;
            unset ($data_tmp[$key]);
    }
    
    if (!$is_filter_success) { //做个记录
        M('member_phone_problem')->add([
            'userid' => $userid,
            'phone' => $data
        ]);
    }
    
    return '[{' .implode('},{', $data_tmp) . '}]';
    
}
    
    
}